//
//  floorTableViewCell.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/5/30.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "floorTableViewCell.h"

@interface floorTableViewCell () <UITableViewDataSource, UITableViewDelegate>
{
    UIImageView *rightImageView;
}

@property (nonatomic, strong) UITableView *myTableView;

@end

@implementation floorTableViewCell

#pragma mark - init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.isShowDetailCell = NO;
        UIImage *image = GET_IAMGE_WITH_NAME(@"Image/tableView/monitor_go");
        rightImageView = [[UIImageView alloc] initWithImage:image];
        [rightImageView setFrame:CGRectMake(self.bounds.size.width - 40, 10, 30, 30)];
        [self addSubview:self.label];
    }
    return self;
}

#pragma mark - UITableViewDelegate  UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify2 = @"cell2";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify2];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify2];
    }
    cell.textLabel.text = [self.dataArray[indexPath.row] objectForKey:@"roomName"];
    [cell addSubview:rightImageView];
    UIImage *leftImage = GET_IAMGE_WITH_NAME(@"Image/tableView/list_room");
    cell.imageView.image = leftImage;
    
    CGSize itemSize = CGSizeMake(30, 30);
    UIGraphicsBeginImageContext(itemSize);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [cell.imageView.image drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.selectCell(indexPath.row, self.dataArray);
}

#pragma mark - method
- (void)showDetailCell
{
    self.isShowDetailCell = YES;
    [self addSubview:self.myTableView];
}

- (void)missDetailCell
{
    self.isShowDetailCell = NO;
    [self.myTableView removeFromSuperview];
}

#pragma mark - setter 和 getter
- (UILabel *)label
{
    if(!_label){
        _label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.bounds.size.width - 10, 50)];
        _label.text = @"";
        _label.textAlignment = NSTextAlignmentLeft;
    }
    return _label;
}

- (UITableView *)myTableView
{
    if(!_myTableView){
        CGRect rect = [UIScreen mainScreen].bounds;
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, rect.size.width, self.dataArray.count * 50) style:UITableViewStylePlain];
        _myTableView.tableFooterView = [[UIView alloc] init];
        _myTableView.backgroundColor = [UIColor whiteColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}

@end
